package com.qyhl.module_activities.act.player.newlist;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ErrorCode;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ActNewVoteModel implements ActNewVoteContract.ActNewVoteModel {
    private ActNewVotePresenter mPresenter;

    ActNewVoteModel(ActNewVotePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void doVote(String actId, String userName, JsonArray playVote) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = ActUrl.ACTIVITY_NEW_VOTE + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "actId=" + actId + "&userName=" + userName +
                        "&siteId=" + CommonUtils.getInstance().getSite() + "&playVote=" + playVote))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.onVoteError("无网络连接，请检查您的网络...");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult<String> apiResult = new Gson().fromJson(s, ApiResult.class);
                        switch (apiResult.getCode()) {
                            case ErrorCode.VOTE_STATUS_1:
                                mPresenter.onVoteError("您已用完今日票数");
                                break;
                            case ErrorCode.VOTE_STATUS_2:
                                mPresenter.onVoteError("本次活动您的票数已用完");
                                break;
                            case ErrorCode.VOTE_STATUS_4:
                            case ErrorCode.VOTE_STATUS_5:
                                mPresenter.onVoteError("对同一选手只能投一票");
                                break;
                            case ErrorCode.SUCCESS:
                            case ErrorCode.COIN_ERROR:
                                mPresenter.onVoteSuccess("no_coin");
                                break;
                            case ErrorCode.COIN_SUCCESS:
                                String data;
                                try {
                                    data = apiResult.getData();
                                } catch (Exception e) {
                                    data = "0";
                                }

                                mPresenter.onVoteSuccess(data);
                                break;
                            default:
                                mPresenter.onVoteError(apiResult.getMessage());
                                break;
                        }
                    }
                });
    }
}
